package com.hc.weather.job;

import com.hc.weather.domain.City;
import com.hc.weather.domain.WeatherResponse;
import com.hc.weather.service.CityService;
import com.hc.weather.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/*
*这是一个定时任务
* */
@Service
public class TimerTasklob {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CityService cityService; //获取城市列表数据
    @Autowired
    WeatherService weatherService;

    @Scheduled(cron = "* * * 15 * *")//每隔10,30秒触发一次
    public void cacheWeatherDataJob(){
        System.out.println("我在缓存天气数据!!!!!!"+new Date());

        //抓取最新的天气数据
        List<City> cityList = cityService.cityList();

        for (City c : cityList) {
            WeatherResponse weatherResponse = weatherService.fetchWeatherData(c.getCityid());
            System.out.println("weatherResponse:"+weatherResponse);

            //抓到天气数据后Redis数据库
            redisTemplate.opsForHash().put("hechi_weather_data",c.getCityid(),weatherResponse);

        }
    }
}
